package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tongatong
 * @name CustomerServiceImpl
 * @description
 * @date 2021/10/15
 * @time 10:23
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerDao customerDao;

    @Override
    public PageInfo<Customer> list(Integer page, Integer rows, String customerName) {
        PageHelper.startPage(page, rows);
        List<Customer> customerList = customerDao.selectByName(customerName);
        PageInfo<Customer> pageInfo = new PageInfo<>(customerList);
        return pageInfo;
    }

    @Override
    public void save(Customer customer, Integer customerId) {
        if (null == customerId) {
            customerDao.insertCustomerById(customer);
        } else {
            customerDao.updateCustomerById(customer);
        }

    }

    @Override
    public void delete(String ids) {
        String regex = ",";
        customerDao.deleteCustomerByIds(ids.split(regex));
    }
}
